home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6141 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  101 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A Linked List Problem
  5. Date: 22 Feb 1996 19:14:30 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4gifam$2ut@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. SHAHZAD ANJUM MALIK <X60CC@CUNYVM.CUNY.EDU> in
  11. <96053.015114X60CC@CUNYVM.CUNY.EDU> asks:
  12.  
  13.  
  14. >     The problem is that the complier (MSC 6.00) gives syntax error on the
  15. >     2nd last line. I have been trying to remove the mistake for several
  16. >     days but useless. The error for "current->Mag = current->Mag.nextMag"
  17. >     is " Erro No. '=' incompatible types...
  18.  
  19. In the revision of your code below, there is a comment regarding this
  20. (correct) diagnostic.
  21.  
  22. There are several other errors and some obfuscation in your code, some
  23. of which have been fixed.  The changes that I have made are marked with
  24. comments beginning with `/* mha -'.
  25.  
  26. The addition of explicit return and return type to main() are stylistic,
  27. as is the change in the malloc() invocation.  I have avoided other
  28. stylistic changes, but these are well worth incorporating into your
  29. general coding style and may save you from errors you might otherwise
  30. never find.  You should get the FAQ from rtfm.mit.edu and read why you
  31. should avoid gets() and how you can do this.
  32.  
  33. [X60CC's original code with revisions noted with `/* mha -']
  34.  
  35. #include <stdlib.h>             /* mha - added */
  36. #include <stdio.h>              /* mha - added */
  37. /* mha - You may have #included these in your original code, but they
  38.  * are missing here and the (unneeded) cast of malloc() in your code
  39.  * suggests that stdlib.h has not been included */
  40.  
  41. struct CustName {
  42.     char LastName[15];
  43.     char IDnum[5];              /* mha - was `char ID#[5];'. `#' is not
  44.                                  * a legal constituent character in a
  45.                                  * C identifier. */
  46. };
  47. struct Magazine {
  48.     char MagName[20];
  49.     struct Magazine *nextMag;
  50. };
  51.  
  52. struct CustProfile {
  53.     struct CustName Name;
  54.     struct Magazine Mag;
  55.     struct CustProfile *next;
  56. };
  57. typedef struct CustProfile ELEMENT;
  58. typedef ELEMENT *LINK;
  59.  
  60.  
  61. int main()
  62. {                               /* mha - added explicit return type */
  63.     LINK current, head;
  64.     LINK NewNode;               /* mha - added.  You _must_ declare
  65.                                  * variables before use. */
  66.     head = NULL;
  67.     NewNode = malloc(sizeof *NewNode);
  68.     /* mha - was `NewNode = (LINK) malloc(sizeof(ELEMENT));' */
  69.  
  70.     if (head == NULL)
  71.         head = current = NewNode;
  72.     else {
  73.         current = head;
  74.         while (current->next != NULL)   /* mha - was `! =' */
  75.             current = current->next;
  76.         current->next = NewNode;
  77.         current = NewNode;
  78.     }
  79.  
  80.     printf("Enter Customer Name: ");
  81.     gets(current->Name.LastName);
  82.     printf("Enter Mag Name");
  83.     gets(current->Mag.MagName);
  84.     printf("Enter Second Mag Name");
  85.     gets(current->Mag.nextMag->MagName);
  86.  
  87.     current->Mag = *(current->Mag.nextMag);
  88.         /* mha - was `current->Mag = current->Mag.nextMag;'.
  89.          * `current->Mag' is a `struct Magazine', while
  90.          * `current->Mag.nextMag' is `struct Magazine *'. I don't know
  91.          * what you intend to be doing, but the replacement is at least
  92.          * legal.  */
  93.  
  94.     current->next = NULL;
  95.     return 0;                   /* mha - added explicit return */
  96. }
  97.                                                                                             
  98. --
  99. * Martin Ambuhl       net: mambuhl@ripco.com
  100. * Chicago, IL (USA)    
  101.